package ding;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class CreateMappingFile {


    private static Connection conn;

    static {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                conn = DriverManager.getConnection("jdbc:mysql://192.168.2.251:3306/mdm?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8", "glsh", "glsh"); //创建连接
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, SQLException {

        File file = new File("C:\\Users\\dinglei\\Desktop\\映射关系.txt");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter writer = new FileWriter(file);

        String s =
                "主表\ttc_md_personnel\n"+
                "合同信息\ttc_md_personnel_contract\n" +
                "学历信息\ttc_md_personnel_ducation\n" +
                "家庭信息\ttc_md_personnel_family\n" +
                "出国（出境）情况信息\ttc_md_personnel_goabroad_situation\n" +
                "语言能力信息\ttc_md_personnel_linguistic_competence\n" +
                "兼职借用信息\ttc_md_personnel_parttime_loan\n" +
                "参加党派记录信息\ttc_md_personnel_partygroups_record\n" +
                "任职信息信息\ttc_md_personnel_post_information\n" +
                "执业资格信息\ttc_md_personnel_practis_qualification\n" +
                "处分情况信息\ttc_md_personnel_punishment_situation\n" +
                "履历记录信息\ttc_md_personnel_resume_record\n" +
                "奖励情况信息\ttc_md_personnel_reward_situation\n" +
                "班子情况信息\ttc_md_personnel_team_situation\n" +
                "专业技术职务信息\ttc_md_personnel_technical_position\n" +
                "培训情况信息\ttc_md_personnel_training_situation\n" +
                "工人技术等级信息\ttc_md_personnel_workers_technical_level\n";


        String[] split = s.split("\n");


        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);


        for (String line : split) {
            String[] strings = line.split("\t");
            String name = strings[0];
            String code = strings[1];


            ResultSet resultSet = stmt.executeQuery("SELECT\n" +
                    "    COLUMN_NAME columnName,\n" +
                    "    COLUMN_COMMENT COMMENT\n" +
                    "FROM\n" +
                    "  INFORMATION_SCHEMA.COLUMNS\n" +
                    "WHERE\n" +
                    "  table_schema = 'mdm'\n" +
                    "AND\n" +
                    "  table_name = '" + code + "'");


            writer.write(code + "\t" +  name+ "\n");

            while (resultSet.next()) {
                String a = resultSet.getString(1);
                String b = resultSet.getString(2);
                writer.write(a + "\t" + b + "\n");
            }

            writer.write("\n\n");

        }


        writer.flush();

        writer.close();

    }


}
